package hot100;

import java.util.LinkedList;
import java.util.List;

public class Solution124 {
    int max = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }

    private int dfs(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = Math.max(dfs(root.left), 0);
        int right = Math.max(dfs(root.right), 0);
        int path = left + right + root.val;
        max = Math.max(max, path);
        return root.val + Math.max(left, right);
    }
}
